package com.pro.addresslist.dal.bll;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.pro.addresslist.dal.model.ContactInfo;
import com.pro.addresslist.tools.JSONHelper;
import com.pro.addresslist.tools.URLUtil;
import com.pro.addresslist.tools.EncryptInterface;
import com.pro.addresslist.tools.EncryptRequest;
import com.pro.addresslist.tools.constantUtil;

public class ContactInfoBLL {
	
	/***
	 * 查询联系人（未分组的，也就是地域和归口为空）
	 */
	public static void SelectContactByNoGroup(Context mContext,final Handler mainHandel){
		final List<ContactInfo> list=new ArrayList<ContactInfo>();
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "SelectContactByNoGroup");
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forContact1, "SelectContactByNoGroup", mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 JSONArray jsonArray = result.optJSONArray("result");
				 
				 if(jsonArray!=null){
					 ContactInfo[] uu=null;
					 uu=JSONHelper.parseArray(jsonArray, ContactInfo.class);
					 for (ContactInfo contactInfo : uu) {
						 list.add(contactInfo);
				        	Log.i("bt","SelectContactByNoGroup+value==>"+contactInfo.get_Name() +">"+contactInfo.get_ContactID());
					}
//					 Intent intent = new Intent(); 
//		             Bundle bundle = new Bundle();
//		             bundle.putSerializable("parameter",(Serializable)list);
//		             intent.putExtras(bundle);
//		             intent.setAction("lbs_SelectContactByNoGroup");  
//		             mContext.sendBroadcast(intent);
					 Message msg = new Message();
					 msg.obj = list;
					 msg.what = constantUtil.Num_SelectContactByNoGroup;
					 mainHandel.sendMessage(msg);	 
				 }
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
	        	Log.i("bt","SelectContactByNoGroup+err==>"+error.getMessage());

			}
		});
	}
	/**
	 * 查询联系人（所有）
	 */
	public static void SelectContactByAll(Context mContext,final Handler mainHandel){
		final List<ContactInfo> list=new ArrayList<ContactInfo>();
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "SelectContactByAll");
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forContact1, "SelectContactByAll", mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 JSONArray jsonArray = result.optJSONArray("result");
				 if(jsonArray!=null){
					 ContactInfo[] uu=null;
					 uu=JSONHelper.parseArray(jsonArray, ContactInfo.class);
					 for (ContactInfo contactInfo : uu) {
						 list.add(contactInfo);
				        	Log.i("bt","SelectContactByAll+value==>"+contactInfo.get_Name() +">"+contactInfo.get_ContactID());
					}
//					 Intent intent = new Intent(); 
//		             Bundle bundle = new Bundle();
//		             bundle.putSerializable("parameter",(Serializable)list);
//		             intent.putExtras(bundle);
//		             intent.setAction("lbs_SelectContactByAll");  
//		             mContext.sendBroadcast(intent);
					 Message msg = new Message();
					 msg.obj = list;
					 msg.what = constantUtil.Num_SelectContactByAll;
					 mainHandel.sendMessage(msg);
				 }
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
	        	Log.i("bt","SelectContactByAll+err==>"+error.getMessage());

			}
		});
	}
	/**
	 * 查询联系人（姓名，模糊查询）
	 */
	public static void SelectContactByName(Context mContext,String name){
		final List<ContactInfo> list=new ArrayList<ContactInfo>();
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "SelectContactByName");
		mMap.put("name", name);
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forContact1, "SelectContactByName", mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 JSONArray jsonArray = result.optJSONArray("result");
				 if(jsonArray!=null){
					 ContactInfo[] uu=null;
					 uu=JSONHelper.parseArray(jsonArray, ContactInfo.class);
					 for (ContactInfo contactInfo : uu) {
						 list.add(contactInfo);
				        	Log.i("bt","SelectContactByName+value==>"+contactInfo.get_Name() +">"+contactInfo.get_ContactID());
					}
					 Intent intent = new Intent(); 
		             Bundle bundle = new Bundle();
		             bundle.putSerializable("parameter",(Serializable)list);
		             intent.putExtras(bundle);
		             intent.setAction("lbs_SelectContactByName");  
		             mContext.sendBroadcast(intent);
				 }
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
	        	Log.i("bt","SelectContactByName+err==>"+error.getMessage());

			}
		});
	}
	/**
	 * 查询联系人（归口，模糊查询）
	 */
	public static void SelectContactBygk(Context mContext,String gk){
		final List<ContactInfo> list=new ArrayList<ContactInfo>();
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "SelectContactBygk");
		mMap.put("gk", gk);
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forContact1, "SelectContactBygk", mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 JSONArray jsonArray = result.optJSONArray("result");
				 if(jsonArray!=null){
					 ContactInfo[] uu=null;
					 uu=JSONHelper.parseArray(jsonArray, ContactInfo.class);
					 for (ContactInfo contactInfo : uu) {
						 list.add(contactInfo);
				        	Log.i("bt","SelectContactBygk+value==>"+contactInfo.get_Name() +">"+contactInfo.get_ContactID());
					}
					 Intent intent = new Intent(); 
		             Bundle bundle = new Bundle();
		             bundle.putSerializable("parameter",(Serializable)list);
		             intent.putExtras(bundle);
		             intent.setAction("lbs_SelectContactBygk");  
		             mContext.sendBroadcast(intent);
				 }
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
	        	Log.i("bt","SelectContactBygk+err==>"+error.getMessage());

			}
		});
	}
	/**
	 * 查询联系人（地域，模糊查询）
	 */
	public static void SelectContactBydy(Context mContext,String dy){
		final List<ContactInfo> list=new ArrayList<ContactInfo>();
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "SelectContactBydy");
		mMap.put("dy", dy);
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forContact1, "SelectContactBydy"+dy, mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 JSONArray jsonArray = result.optJSONArray("result");
				 if(jsonArray!=null){
					 ContactInfo[] uu=null;
					 uu=JSONHelper.parseArray(jsonArray, ContactInfo.class);
					 for (ContactInfo contactInfo : uu) {
						 list.add(contactInfo);
				        	Log.i("bt","SelectContactBydy+value==>"+contactInfo.get_Name() +">"+contactInfo.get_ContactID());
					}
					 Intent intent = new Intent(); 
		             Bundle bundle = new Bundle();
		             bundle.putSerializable("parameter",(Serializable)list);
		             intent.putExtras(bundle);
		             intent.setAction("lbs_SelectContactBydy");  
		             mContext.sendBroadcast(intent);
				 }
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
	        	Log.i("bt","SelectContactBydy+err==>"+error.getMessage());

			}
		});
	}
	/**
	 * 计算联系人数量（归口）
	 */
	public static void SelectContactNumBygk(Context mContext,final String gk){
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "SelectContactNumBygk");
		mMap.put("gk", gk);
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forContact1, "SelectContactNumBygk"+gk, mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 String json = result.optString("result");
		        	Log.i("bt","SelectContactNumBygk+value==>"+json);

				 Intent intent = new Intent(); 
	             Bundle bundle = new Bundle();
	             bundle.putSerializable("parameter",json);
	             bundle.putSerializable("gk",gk);
	             intent.putExtras(bundle);
	             intent.setAction("lbs_SelectContactNumBygk");  
	             mContext.sendBroadcast(intent); 
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
			}
		});
	}
	/**
	 * 计算联系人数量（地域）
	 */
	public static void SelectContactNumBydy(Context mContext,final String dy){
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "SelectContactNumBydy");
		mMap.put("dy", dy);
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forContact1, "SelectContactNumBydy"+dy, mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 String json = result.optString("result");
		        	Log.i("bt","SelectContactNumBydy+value==>"+json);

				 Intent intent = new Intent(); 
	             Bundle bundle = new Bundle();
	             bundle.putSerializable("parameter",json);
	             bundle.putSerializable("dy",dy);
	             intent.putExtras(bundle);
	             intent.setAction("lbs_SelectContactNumBydy");  
	             mContext.sendBroadcast(intent);
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
			}
		});
	}
	/**
	 * 新建联系人
	 */
	public static void AddContact(Context mContext,ContactInfo contactInfo,final int id){
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "AddContact");
		mMap.put("contactInfo", JSONHelper.toJSON(contactInfo));
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forContact1, "AddContact"+id, mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {		
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 String json = result.optString("result");
		        	Log.i("bt","AddContact+value==>"+json);
				 Intent intent = new Intent(); 
	             Bundle bundle = new Bundle();
	             bundle.putSerializable("parameter",json);
	             bundle.putSerializable("id",id);
	             intent.putExtras(bundle);
	             intent.setAction("lbs_AddContact");  
	             mContext.sendBroadcast(intent);
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
			}
		});
	}
	/**
	 * 编辑联系人
	 */
	public static void UpdataContact(Context mContext,ContactInfo contactInfo,final int id){
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "UpdataContact");
		mMap.put("contactInfo", JSONHelper.toJSON(contactInfo));
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forContact1, "UpdataContact"+id, mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 String json = result.optString("result");
		        	Log.i("bt","UpdataContact+value==>"+json);

				 Intent intent = new Intent(); 
	             Bundle bundle = new Bundle();
	             bundle.putSerializable("parameter",json);
	             bundle.putSerializable("id",id);
	             intent.putExtras(bundle);
	             intent.setAction("lbs_UpdataContact");  
	             mContext.sendBroadcast(intent);
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
				Log.i("bt","UpdataContact+err==>"+error.toString());
			}
		});
	}
	/**
	 * 删除联系人
	 */
	public static void DelContactByID(Context mContext,String contactID,final int id,final Handler mainHandler){
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "DelContactByID");
		mMap.put("contactID", contactID);
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forContact1, "DelContactByID"+id, mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 String json = result.optString("result");
		        	Log.i("bt","DelContactByID+value==>"+json);

//				 Intent intent = new Intent(); 
//	             Bundle bundle = new Bundle();
//	             bundle.putSerializable("parameter",json);
//	             bundle.putSerializable("id",id);
//	             intent.putExtras(bundle);
//	             intent.setAction("lbs_DelContactByID");  
//	             mContext.sendBroadcast(intent);
		        	Message msg = new Message();
					msg.obj = json;
					msg.arg1=id;
					msg.what = constantUtil.Num_DelContactByID;
					mainHandler.sendMessage(msg);
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
			}
		});
	}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>分割线，以下用接口2>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	/**
	 * 查询联系人（单人）
	 */
	public static void SelectContactByID(Context mContext,final String contactID,final int type){
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "SelectContactByID");
		mMap.put("contactID", contactID);
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forContact2, "SelectContactByID"+contactID, mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 JSONObject jsonObject = result.optJSONObject("result");
				 if(jsonObject!=null){
					 ContactInfo uu=new ContactInfo();

					 try {
						uu=JSONHelper.parseObject(jsonObject, ContactInfo.class);
			        	Log.i("bt","SelectContactByID+value==>"+uu.get_Name() +">"+uu.get_ContactID());

					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					 Intent intent = new Intent(); 
		             Bundle bundle = new Bundle();
		             bundle.putSerializable("parameter",uu);
		             bundle.putSerializable("contactID",contactID);
		             bundle.putSerializable("type",type);
		             intent.putExtras(bundle);
		             intent.setAction("lbs_SelectContactByID");  
		             mContext.sendBroadcast(intent);
				 }
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
			}
		});
	}

	/**
	 * 查询联系人（多人）
	 */
	public static void SelectContactByIDs(Context mContext,String[] IDs){
		final List<ContactInfo> list=new ArrayList<ContactInfo>();
		String id="";
		for(String str:IDs){
			id+=str+",";
		}
		//id=id.substring(1,id.length()-1);
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "SelectContactByIDs");
		mMap.put("contactID", id);
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forContact2, "SelectContactByIDs", mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 JSONArray jsonArray = result.optJSONArray("result");
				 if(jsonArray!=null){
					 ContactInfo[] uu=null;
					 uu=JSONHelper.parseArray(jsonArray, ContactInfo.class);
					 for (ContactInfo contactInfo : uu) {
						 list.add(contactInfo);
				        	Log.i("bt","SelectContactByIDs+value==>"+contactInfo.get_Name() +">"+contactInfo.get_ContactID());
					}
					 Intent intent = new Intent(); 
		             Bundle bundle = new Bundle();
		             bundle.putSerializable("parameter",(Serializable)list);
		             intent.putExtras(bundle);
		             intent.setAction("lbs_SelectContactByIDs");  
		             mContext.sendBroadcast(intent);
				 }
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
			}
		});
	}
	/**
	 * 修改联系人地域
	 */
	public static void UpdataContactBydy(Context mContext,String contactID,String dy){
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "UpdataContactBydy");
		mMap.put("contactID", contactID);
		mMap.put("dy", dy);
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forContact2, "UpdataContactBydy", mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 String json = result.optString("result");
		        	Log.i("bt","SelectContactNumBydy+value==>"+json);

				 Intent intent = new Intent(); 
	             Bundle bundle = new Bundle();
	             bundle.putSerializable("parameter",json);
	             intent.putExtras(bundle);
	             intent.setAction("lbs_UpdataContactBydy");  
	             mContext.sendBroadcast(intent);
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
			}
		});
	}
	/**
	 *修改联系人归口
	 */
	public static void UpdataContactBygk(Context mContext,String contactID,String gk){
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "UpdataContactBygk");
		mMap.put("contactID", contactID);
		mMap.put("gk", gk);
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forContact2, "UpdataContactBygk", mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 String json = result.optString("result");
		        	Log.i("bt","SelectContactNumBydy+value==>"+json);

				 Intent intent = new Intent(); 
	             Bundle bundle = new Bundle();
	             bundle.putSerializable("parameter",json);
	             intent.putExtras(bundle);
	             intent.setAction("lbs_UpdataContactBygk");  
	             mContext.sendBroadcast(intent);
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
			}
		});
	}
	/**
	 * 查询联系人生日(按未来一周的查)
	 */
	public static void SelectContactBySR(Context mContext){
		final List<ContactInfo> list=new ArrayList<ContactInfo>();
		Map<String, String> mMap=new HashMap<String, String>();
		mMap.put("action", "SelectContactBySR");
		EncryptRequest.RequestJsonPost(mContext, URLUtil.Url_forContact2, "SelectContactBySR", mMap, 
				new EncryptInterface(mContext, EncryptInterface.mjsonObjListener, EncryptInterface.mErrorListtener) {
			
			@Override
			public void onMySuccess(JSONObject result) {
				if(result.has("result"))
				{
				 JSONArray jsonArray = result.optJSONArray("result");
				 if(jsonArray!=null){
					 ContactInfo[] uu=null;
					 uu=JSONHelper.parseArray(jsonArray, ContactInfo.class);
					 for (ContactInfo contactInfo : uu) {
						 list.add(contactInfo);
				        	Log.i("bt","SelectContactByIDs+value==>"+contactInfo.get_Name() +">"+contactInfo.get_ContactID());
					}
					 Intent intent = new Intent(); 
		             Bundle bundle = new Bundle();
		             bundle.putSerializable("parameter",(Serializable)list);
		             intent.putExtras(bundle);
		             intent.setAction("lbs_SelectContactBySR");  
		             mContext.sendBroadcast(intent);
				 }
				} 
			}
			
			@Override
			public void onMyError(VolleyError error) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, "服务器错误！", Toast.LENGTH_SHORT).show();
	        	Log.i("bt","SelectContactByIDs+value==>"+error.getMessage());

			}
		});
	}
}
